﻿package com.fyGame.fyMap
{
    import __AS3__.vec.*;
    import com.gengine.gc.*;
    import flash.utils.*;

    public class FyMapInfo extends Object
    {
        public var mapId:int;
        public var pieceWidth:int;
        public var pieceHeight:int;
        public var gridXNum:int;
        public var gridYNum:int;
        public var gridWidth:int;
        public var gridHeight:int;
        public var connectedPointY:int;
        public var isDoubleMap:Boolean = true;
        public var bgMapWidth:int = 2000;
        public var bgMapHeight:int = 2000;
        public var bgMapName:String = "";
        public var mapPicDict:Dictionary;
        public var mapData:Array;
        public var v:Vector.<int>;

        public function FyMapInfo()
        {
            this.mapData = new Array();
            this.v = new Vector.<int>;
            return;
        }// end function

        public function getPicType(param1:int, param2:int) : int
        {
            if (!this.isDoubleMap)
            {
                return MapPicType.JPG;
            }
            if (this.mapPicDict == null)
            {
                return MapPicType.JPG;
            }
            if (this.mapPicDict[param2 * 1000 + param1] == 0)
            {
                return MapPicType.BLANK;
            }
            if (this.mapPicDict[param2 * 1000 + param1] == 1)
            {
                return MapPicType.JPG;
            }
            return MapPicType.PNG;
        }// end function

        public function read(param1:ByteArray, param2:Boolean = true, param3:Boolean = false) : void
        {
            var _loc_6:Array = null;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            var _loc_10:int = 0;
            var _loc_11:int = 0;
            if (param2)
            {
                param1.uncompress();
            }
            param1.endian = Endian.LITTLE_ENDIAN;
            param1.position = 0;
            this.mapId = param1.readInt();
            this.pieceWidth = param1.readInt();
            this.pieceHeight = param1.readInt();
            this.gridXNum = param1.readInt();
            this.gridYNum = param1.readInt();
            this.gridWidth = param1.readInt();
            this.gridHeight = param1.readInt();
            this.connectedPointY = param1.readInt();
            if (!param3)
            {
                this.isDoubleMap = param1.readBoolean();
            }
            var _loc_4:* = this.readSize(param1);
            if (_loc_4 > 1000)
            {
                throw new Error("地图数据出错1!FyMapInfo.as");
            }
            var _loc_5:int = 0;
            while (_loc_5 < _loc_4)
            {
                
                _loc_6 = new Array();
                _loc_7 = this.readSize(param1);
                if (_loc_7 > 1000)
                {
                    throw new Error("地图数据出错2!FyMapInfo.as");
                }
                _loc_8 = 0;
                while (_loc_8 < _loc_7)
                {
                    
                    _loc_6[_loc_8] = param1.readByte();
                    _loc_8++;
                }
                this.mapData[_loc_5] = _loc_6;
                _loc_5++;
            }
            if (!param3)
            {
                _loc_4 = this.readSize(param1);
                if (_loc_4 > 10000)
                {
                    throw new Error("地图数据出错3!FyMapInfo.as");
                }
                _loc_9 = 0;
                while (_loc_9 < _loc_4)
                {
                    
                    _loc_10 = param1.readInt();
                    _loc_11 = _loc_10 % 1000000;
                    if (this.mapPicDict == null)
                    {
                        this.mapPicDict = new Dictionary();
                    }
                    this.mapPicDict[_loc_11] = int(_loc_10 / 1000000);
                    _loc_9++;
                }
            }
            return;
        }// end function

        public function write(param1:Boolean = true, param2:Boolean = false) : ByteArray
        {
            var _loc_4:Array = null;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:String = null;
            var _loc_9:int = 0;
            var _loc_3:* = new GByteArray();
            _loc_3.endian = Endian.LITTLE_ENDIAN;
            _loc_3.writeInt(this.mapId);
            _loc_3.writeInt(this.pieceWidth);
            _loc_3.writeInt(this.pieceHeight);
            _loc_3.writeInt(this.gridXNum);
            _loc_3.writeInt(this.gridYNum);
            _loc_3.writeInt(this.gridWidth);
            _loc_3.writeInt(this.gridHeight);
            _loc_3.writeInt(this.connectedPointY);
            if (!param2)
            {
                _loc_3.writeBoolean(this.isDoubleMap);
            }
            this.writeSize(_loc_3, this.mapData.length);
            var _loc_5:int = 0;
            while (_loc_5 < this.mapData.length)
            {
                
                _loc_4 = this.mapData[_loc_5];
                this.writeSize(_loc_3, _loc_4.length);
                _loc_6 = 0;
                while (_loc_6 < _loc_4.length)
                {
                    
                    _loc_3.writeByte(_loc_4[_loc_6]);
                    _loc_6++;
                }
                _loc_5++;
            }
            if (!param2)
            {
                _loc_7 = 0;
                for each (_loc_8 in this.mapPicDict)
                {
                    
                    _loc_7++;
                }
                this.writeSize(_loc_3, _loc_7);
                for (_loc_8 in this.mapPicDict)
                {
                    
                    _loc_9 = int(this.mapPicDict[_loc_8]) * 1000000 + int(_loc_8);
                    _loc_3.writeInt(_loc_9);
                }
                if (param1)
                {
                    _loc_3.compress();
                }
            }
            return _loc_3;
        }// end function

        public function writeSize(param1:ByteArray, param2:int) : void
        {
            if (param2 < 255)
            {
                param1.writeByte(param2);
            }
            else
            {
                param1.writeByte(255);
                param1.writeInt(param2);
            }
            return;
        }// end function

        public function readSize(param1:ByteArray) : int
        {
            var _loc_2:* = param1.readUnsignedByte();
            if (_loc_2 == 255)
            {
                return param1.readInt();
            }
            return _loc_2;
        }// end function

    }
}
